package com.example.administrator.yanfoxconn.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.LMExceListAdapter;
import com.example.administrator.yanfoxconn.adapter.LMListAdapter;
import com.example.administrator.yanfoxconn.bean.LMIE8S;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.example.administrator.yanfoxconn.widget.MyListView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description IE8S 稽核 部門列表
 * @Author song
 * @Date 2/19/22 9:22 AM
 */
public class LMList2Activity extends BaseActivity  implements View.OnClickListener{
    private final int MESSAGE_SET_JC=111;

    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.tv_one)
    TextView tvOne;//標題
    @BindView(R.id.tv_two)
    TextView tvTwo;//標題
    @BindView(R.id.tv_three)
    TextView tvThree;//標題
    @BindView(R.id.mlv)
    MyListView mlv;

    private String from,flag1,product,zxYear="";//來源類別
    private List<LMIE8S> jcList;
    private LMListAdapter lmListAdapter;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lm_list);
        ButterKnife.bind(this);

        btnBack.setOnClickListener(this);
        tvTitle.setText(getIntent().getStringExtra("title"));
        flag1 = getIntent().getStringExtra("flag1");
        product = getIntent().getStringExtra("product");

        if (flag1.equals("jcjh")) {
           // getList(FoxContext.getInstance().getFlag(), product, FoxContext.getInstance().getDepart(), flag1);
            tvOne.setText("部門");
            tvTwo.setText("上半月");
            tvThree.setText("下半月");
            mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent2 = new Intent(LMList2Activity.this, LMExceListActivity.class);
                    intent2.putExtra("product",product);
                    intent2.putExtra("depart",jcList.get(position).getDepart());
                    intent2.putExtra("flag1",flag1);

                    startActivity(intent2);
                }
            });
        }else{
            from=getIntent().getStringExtra("from");
            if (flag1.equals("jdjh")){
                zxYear = getIntent().getStringExtra("zx_year");
            }
            //getJdZxList(FoxContext.getInstance().getFlag(), product, FoxContext.getInstance().getDepart(), flag1,from,zxYear);
            tvOne.setText("部門");
            tvTwo.setVisibility(View.GONE);
            tvThree.setVisibility(View.GONE);
            mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent2 = new Intent(LMList2Activity.this,LMExceListActivity.class);
                    intent2.putExtra("product",product);
                    intent2.putExtra("depart",jcList.get(position).getDepart());
                    intent2.putExtra("flag1",flag1);
                    intent2.putExtra("zx_type",from);
                    intent2.putExtra("zx_year",zxYear);
                    startActivity(intent2);
                }
            });
        }

    }

    /*IE8S 獲取數據*/
    public void getList(String flag,String product,String depart,String flag1) {

        showDialog();
        final String url = Constants.HTTP_LM_GET_NUMBER2  ;

        JsonObject object = new JsonObject();
        object.addProperty("flag", flag);
        object.addProperty("product", product);
        object.addProperty("depart", depart);
        object.addProperty("flag1", flag1);

        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "object==fff===" + object.toString());
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObjectData = jsonObject.get("data").getAsJsonObject();
                            JsonArray array = jsonObjectData.get("getList1").getAsJsonArray();
                            jcList = new ArrayList<LMIE8S>();
                            for (JsonElement type : array) {
                                LMIE8S humi = gson.fromJson(type, LMIE8S.class);
                                jcList.add(humi);
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_JC;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    /*IE8S 獲取jdjh.zxjh數據*/
    public void getJdZxList(String flag,String product,String depart,String flag1,String zx_type,String zx_year) {

        showDialog();
        final String url = Constants.HTTP_LM_GET_JD_DEPART  ;

        JsonObject object = new JsonObject();
        object.addProperty("flag", flag);
        object.addProperty("product", product);
        object.addProperty("depart", depart);
        object.addProperty("flag1", flag1);
        object.addProperty("zx_type", zx_type);
        object.addProperty("zx_year", zx_year);

        //開啟一個新執行緒，向伺服器上傳資料
        new Thread() {
            public void run() {
                //把网络访问的代码放在这里
                try {
                    Log.e("---------", "==fff===" + url);
                    String result = HttpConnectionUtil.doPostJsonObject(url, object);
                    Gson gson = new Gson();
                    Log.e("---------", "object==fff===" + object.toString());
                    if (result != null) {
                        dismissDialog();
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("code").getAsString();
                        if (errCode.equals("200")) {
                            JsonObject jsonObjectData = jsonObject.get("data").getAsJsonObject();
                            JsonArray array = jsonObjectData.get("getList1").getAsJsonArray();
                            jcList = new ArrayList<LMIE8S>();
                            for (JsonElement type : array) {
                                LMIE8S humi = gson.fromJson(type, LMIE8S.class);
                                jcList.add(humi);
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_JC;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        } else{
                            Message message = new Message();
                            message.what = Constants.MESSAGE_TOAST;
                            message.obj = jsonObject.get("message").getAsString();
                            mHandler.sendMessage(message);
                        }
                    }else{
                        Message message = new Message();
                        message.what = Constants.MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SET_JC:
                    lmListAdapter = new LMListAdapter(LMList2Activity.this,jcList,flag1,"two");
                    mlv.setAdapter(lmListAdapter);
                    setListViewHeightBasedOnChildren(mlv);
                    break;
                case Constants.MESSAGE_TOAST://Toast
                    ToastUtils.showShort(LMList2Activity.this, msg.obj.toString());
                    break;
                case Constants.MESSAGE_NETMISTAKE://Toast
                    ToastUtils.showLong(LMList2Activity.this,R.string.net_mistake);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_title_left:
                finish();
                break;

        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        if (flag1.equals("jcjh")) {
            getList(FoxContext.getInstance().getFlag(), product, FoxContext.getInstance().getDepart(), flag1);
        }else {
            getJdZxList(FoxContext.getInstance().getFlag(), product, FoxContext.getInstance().getDepart(), flag1,from,zxYear);
        }
    }

    /**
     * 用於解決scrollview嵌套listview導致只顯示一條數據的問題
     *
     * @param listView
     */
    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        LMListAdapter listAdapter = (LMListAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

}
